package com.zwr.normal.forkjoin;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class AddTask1  extends RecursiveTask<Integer> {

    int n;
    public AddTask1(int n) {
        this.n = n;
    }


    @Override
    public String toString() {
        return "{" + n + '}';
    }


    @Override
    protected Integer compute() {
        // 如果 n 已经为 1，可以求得结果了
        if (n == 1) {
            System.out.println("join()"+ n);
            return n;
        }
        // 将任务进行拆分(fork)
        AddTask1 t1 = new AddTask1(n - 1);
        t1.fork();
        System.out.println("fork()"+ n+"+"+ t1);



        // 合并(join)结果
        int result = n + t1.join();
        System.out.println("join()"+ n+"+" + t1+"+"+ result);
        return result;
    }

    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool(4);
        System.out.println(pool.invoke(new AddTask1(5)));
    }
}
